{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31.6 Powers of an element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 31.6-1\n",
    "\n",
    "> Draw a table showing the order of every element in $\\mathbb{Z}_{11}^*$. Pick the smallest primitive root $g$ and compute a table giving $\\text{ind}_{11,g}(x)$ for all $x \\in \\mathbb{Z}_{11}^*$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$g = 2$, $\\{1, 2, 4, 8, 5, 10, 9, 7, 3, 6\\}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 31.6-2\n",
    "\n",
    "> Give a modular exponentiation algorithm that examines the bits of $b$ from right to left instead of left to right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def modular_exponentiation(a, b, n):\n",
    "    i, d = 0, 1\n",
    "    while (1 << i) <= b:\n",
    "        if (b & (1 << i)) > 0:\n",
    "            d = (d * a) % n\n",
    "        a = (a * a) % n\n",
    "        i += 1\n",
    "    return d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 31.6-3\n",
    "\n",
    "> Assuming that you know $\\phi(n)$, explain how to compute $a^{-1}~\\text{mod}~n$ for any $a \\in \\mathbb{Z}_n^*$ using the procedure MODULAR-EXPONENTIATION."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{array}{rlll}\n",
    "a^{\\phi(n)} &\\equiv& 1 &(\\text{mod}~n) \\\\\n",
    "a\\cdot a^{\\phi(n)-1} &\\equiv& 1 &(\\text{mod}~n) \\\\\n",
    "a^{-1} &\\equiv& a^{\\phi(n)-1} &(\\text{mod}~n)  \\\\\n",
    "\\end{array}\n",
    "$$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
